<?php
/**
 * 产品原型辅助设计类
 *
 * @author xyma
 *
 */
class ProtoHelper
{
	/**
	 * 取一条完整的原型信息，含属性
	 *
	 * @param $id
	 * @return unknown_type
	 */
	public static function load($id)
	{
		if(!intval($id))
		{
			return false;
		}
		
		$proto	=	Proto::get($id);
		if($proto)
		{
			$proto['attrs']	=	ProtoAttr::getList(array('pro_id'=>$id,'off'=>0));
		}
		return $proto;
	}
	
	/**
	 * 将一个完整的proto数据显示成html样例，可用于做产品信息修改、原型样例显示
	 *
	 * 可替换的变量:
	 * <!--hook_title--> 生成的表单标题 可替换
	 * <!--hook_button--> 生成的表单提交按钮部分可替换
	 *
	 * require jQuery
	 *
	 * @return string
	 */
	public static function pack($proto, $data=array())
	{
		$html	=	'';
		$js		=	'';

		if(!empty($proto['attrs'])) foreach($proto['attrs'] as $attr_id=>$attr)
		{
			$attr_s	= '';	//显示字段名和描述
			$attr_v	= '';	//不显示字段名和描述
			
			$name	= "attr[{$attr_id}]";
			$id		= "attr_{$attr_id}";
			$value	= isset($data[$attr_id]) ? $data[$attr_id] : (
						$attr['h_type']=='1'?array():'' );

			if($attr['type']=='1')	//选择型属性
			{
				$clist	=	Cate::getByPid($attr['hash_id']);
				if($attr['h_type']=='1')
				{
					foreach($clist as $hash)
					{
						$ck	= in_array($hash['hid'], $value) ? 'checked' : '';
						$attr_s	.=	" <nobr><input type='checkbox' name='{$name}[]' id='{$id}_{$hash['hid']}' {$ck}
									value='{$hash['hid']}' /><label for='{$id}_{$hash['hid']}'>{$hash['title']}</label></nobr>&nbsp;";
					}
				}
				else{
					$attr_s	.=	Html::options($clist, $id, $value, 'title', "--{$attr['title']}--", "name='{$name}'");
				}
				unset($clist);
			}
			elseif($attr['type']=='2')	//交集选择型属性
			{
				$pids	= explode(':', $attr['hash_path']);
				if(!isset($pids[1])) $pids[1] = '0';
				$pv1	= isset($data[$pids[0]]) ? $data[$pids[0]] : 0;
				$pv2	= isset($data[$pids[1]]) ? $data[$pids[1]] : 0;
				if($pv1 || $pv2)
					# 检查若CateMix.pidX有值则尝试取交集
				{
					$clist	= CateMix::getMix($pv1, $pv2);
					if($attr['h_type']=='1')
					{
						$tmp	= array();
						foreach($clist as $a){
							$ck	= in_array($a['hid'], $value) ? 'checked' : '';
							$tmp[]	= " <nobr><input name='{$name}[]' id='{$id}{$a['hid']}' value='{$a['hid']}' {$ck}
								type='checkbox' /><label for='{$id}{$a['hid']}'>{$a['title']}</label></nobr>&nbsp;";
						}
						$attr_s	= sprintf("<div id='%s'>%s</div>", $id, join('', $tmp));
						unset($tmp);
					}
					else{
						$attr_s	.=	Html::options($clist, $id, $value, 'title', "--{$attr['title']}--", "name='{$name}'");
					}
					unset($clist);
				}
				else
					# 否则视为新建或proto-prev操作
				{
					$attr_s	= ($attr['h_type']=='1')
								? $attr_s	= "<div id='{$id}'></div>"
								: "<select name='{$name}' id='{$id}'></select>";
				}
				
				$url	= url('mix-find/pid/PID');
				$js	.= <<<JS
	window.URL_FINDMIXED = "{$url}";
	_func = function(){findmixed("attr_{$pids[0]}","attr_{$pids[1]}","{$id}","{$name}","{$attr['h_type']}")};
	$('#attr_{$pids[0]}').change(_func);
	$('#attr_{$pids[1]}').change(_func);
JS;
			}
			else		//字符串型
			{
				$value	=	$value ? addslashes($value) : $attr['def_v'];
				switch($attr['b_type'])
				{
				case '0':
					$attr_v	= "<input type='hidden' name='{$name}' id='{$id}' value='{$value}' />";
					break;
				case '1':
					$attr_s	= "<input type='text' name='{$name}' id='{$id}' value='{$value}' size='10' maxlength='{$attr['maxlen']}' />";
					break;
				case '2':
					$attr_s	= "<input type='text' name='{$name}' id='{$id}' value='{$value}' size='20' maxlength='{$attr['maxlen']}' />";
					break;
				case '3':
					$attr_s	= "<input type='text' name='{$name}' id='{$id}' value='{$value}' size='40' maxlength='{$attr['maxlen']}' />";
					break;
				case '4':
					$attr_s	= "<textarea name='{$name}' id='{$id}' rows='4' cols='46'>{$value}</textarea>";
					$js		.= "maxLen('{$id}',{$attr['maxlen']});";
					break;
				}
			}
			
			if($attr_v){
				$html	.=	$attr_v;
			}
			else{
				$html	.=	<<<HTML
<tr>
	<td class='title'>{$attr['title']}</td>
	<td class='data'>{$attr_s}</td>
	<td class='desc'>{$attr['desc']}</td>
</tr>
HTML;
			}
		}
		if($js){
			$html	.=	"<script>{$js}</script>";
		}
		
		return $html;
	}
	
	/**
	 * 填充一下node.attrs属性数组，生成可用于显示的数据
	 *
	 * @param $data		node.attrs 节点属性数组
	 * @param $pk_code	是否以proto_attr.code做为键名/或以.attr_id为键名
	 * @return array
	 */
	public static function inflateAttr($attrs, $data, $pk_code=true)
	{
		$tmp	= array();
		if(!empty($attrs)) foreach($attrs as $attr_id=>$attr)
		{
			if(in_array($attr['type'], array('1','2')))	//hash id
			{
				if(empty($data[$attr_id])){
					$attr['value']	= '';
				}
				elseif($attr['h_type']=='1'){
					$attr['value']	= join('，',
							Db::get('Cate')->select('title')
							->where(sprintf('hid IN (%s)', join(',',$data[$attr_id])))
							->getCols() );
				}
				else{
					$attr['value']	= Db::get('Cate')->select('title')
							->where(array('hid'=>$data[$attr_id]))->getCol();
				}
			}
			else		//str attr
			{
				$attr['value']	=	$data[$attr_id];
			}
			$tmp[$pk_code?$attr['code']:$attr_id]	= $attr;
		}
		return $tmp;
	}
	
}
